import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
interface IEvent {
    func: Function,
    context: any
}


@ccclass('EventManager')
export default class EventManager {

    private static eventDic: Map<string, Array<IEvent>> = new Map();

    /** 注册事件 */
    static on(eventName: string, func: Function, context: any) {
        if (this.eventDic.has(eventName)) {
            this.eventDic.get(eventName)?.push({ func, context });
        } else {
            this.eventDic.set(eventName, [{ func, context }]);
        }
    }

    /**解绑事件 */
    static off(eventName: string, func: Function) {
        if (this.eventDic.has(eventName)) {
            const index = this.eventDic.get(eventName).findIndex(i => i.func === func);
            index > -1 && this.eventDic.get(eventName).splice(index, 1);
        }
    }

    /** 发送事件 */
    static emit(eventName: string, ...args: any[]) {
        if (this.eventDic.has(eventName)) {
            this.eventDic.get(eventName).forEach(({ func, context }) => {
                context ? func.apply(context, args) : func(...args)
            });
        }
    }
}
/** 事件枚举 */
export enum EVENT_ENUM {
    DRAW_CARDS = "DRAW_CARDS",
    UNDO = "UNDO",
    DISTURB = "DISTURB",
    CARD_CLICKED = "CARD_CLICKED",
}


